package com.woniu.springbootmybatis.controller;

import com.woniu.springbootmybatis.CustomerVo;
import com.woniu.springbootmybatis.pojo.Customer;
import com.woniu.springbootmybatis.service.ICustomerService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cus")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    @ApiOperation(value = "查询所有客户信息",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = List.class,notes = "获取所有客户列表")
    @RequestMapping("/findAll")
    @ResponseBody
    public List<Customer> findAll(){
        return customerService.findAll();
    }
    @ApiOperation(value = "通过id查询客户信息",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = Customer.class,notes = "获取单个客户信息")
    @RequestMapping("/findOne")
    @ResponseBody
    public Customer findOne(@ApiParam(required = true,name = "id",value = "客户编号") @RequestParam Integer id){
        return this.customerService.findOne(id);
    }
    @ApiOperation(value = "增加客户信息",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response= Map.class,notes = "增加客户信息")
    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Object> save(@RequestBody CustomerVo customerVo){
        Customer customer = new Customer(customerVo.getName(),customerVo.getLinkman(),customerVo.getPhone(),customerVo.getCreatedate());
        boolean flag = this.customerService.save(customer);
        Map<String,Object> map = new HashMap<String,Object>();
        if(flag){
            map.put("msg","增加成功");
        }else {
            map.put("msg","增加失败");
        }
        return map;
    }
}
